import { Component, OnInit } from '@angular/core';
import { Quota } from 'src/app/api/appconfig';
import { WellService } from 'src/app/service/well.service';
import { PlanService } from 'src/app/service/plan.service';
import { ConfirmationService, MessageService } from 'primeng/api';

@Component({
    selector: 'app-quota',
    templateUrl: './quota.component.html',
    styleUrls: ['./quota.component.scss'],
    providers: [ConfirmationService, MessageService],
})
export class QuotaComponent implements OnInit {
    display: boolean = false;
    display2: boolean = false;
    year: any = new Date().getFullYear();
    currentItem = {
        licenseVolume: '',
        applicantId: '',
        applicantName: '',
    };
    constructor(
        private wellService: WellService,
        private planService: PlanService,
        private messageService: MessageService,
        private confirmationService: ConfirmationService
    ) {}
    quotaList: any[];
    planValue = {
        downYear: {
            firstPlanVolume: '',
            secondPlanVolume: '',
            totalPlanVolume: '',
        },
        lastYear: {
            firstPlanVolume: '',
            secondPlanVolume: '',
            totalPlanVolume: '',
        },
        currentYear: {
            firstPlanVolume: '',
            secondPlanVolume: '',
            totalPlanVolume: '',
        },
    };
    searchForm = {
        name: '',
    };
    editForm = {
        firstPlanVolume: null,
        secondPlanVolume: null,
    };
    reset() {
        this.searchForm.name = '';
        this.search();
    }
    async search() {
        const res = await this.wellService.fetchVolumeListByConfig({
            applicantName: this.searchForm.name,
        });
        console.log(res);
        if (res.code === '200') {
            this.quotaList = res.data;
            console.log('data', this.quotaList);
        } else {
            this.messageService.add({
                severity: 'error',
                summary: '错误',
                detail: '暂无消息',
            });
        }
    }
    async getVolumeList(item) {
        const res1 = await this.wellService.fetchPrevVolumeByConfig({
            applicantId: item.applicantId,
            planYear: new Date().getFullYear() - 2,
        });
        const res2 = await this.wellService.fetchPrevVolumeByConfig({
            applicantId: item.applicantId,
            planYear: new Date().getFullYear() - 1,
        });
        const res3 = await this.wellService.fetchPrevVolumeByConfig({
            applicantId: item.applicantId,
            planYear: new Date().getFullYear(),
        });
        if (res1.code == 200 && res2.code == 200 && res3.code == 200) {
            this.planValue.currentYear = res3.data;
            this.planValue.lastYear = res2.data;
            this.planValue.downYear = res1.data;
        }
    }
    async edit() {
        console.log(this.editForm);
        const res1 = await this.planService.updateYearPlanById({
            applicantId: this.currentItem.applicantId,
            planVolume: parseFloat(this.editForm.firstPlanVolume),
            planType: 1,
            planYear: new Date().getFullYear() + '',
        });
        const res2 = await this.planService.updateYearPlanById({
            applicantId: this.currentItem.applicantId,
            planVolume: parseFloat(this.editForm.secondPlanVolume),
            planType: 2,
            planYear: new Date().getFullYear() + '',
        });
        if (res1.code === '200' && res2.code === '200') {
            this.display = false;
            this.search();
            this.messageService.add({
                severity: 'info',
                summary: '确认',
                detail: '你已经更新',
            });
            return true;
        }
        this.messageService.add({
            severity: 'error',
            summary: '错误',
            detail: '修改失败',
        });
        return false;
    }
    ngOnInit(): void {
        this.search();
    }
    openDialog1(a) {
        this.display = true;
        this.currentItem = a;
        this.editForm.firstPlanVolume = a.firstPlanVolume;
        this.editForm.secondPlanVolume = a.secondPlanVolume;
    }
    openDialog2(a) {
        this.display2 = true;
        this.currentItem = a;
        this.getVolumeList(a);
    }
}
